package Service;

import Dao.FindByPageDao;
import Table.Book;
import Table.PageBean;

import java.util.ArrayList;

public class FindByPageService {
    public static PageBean<Book> findByPage(String currentPage1, String rows1) {
        int currentPage = Integer.parseInt(currentPage1);
        int rows = Integer.parseInt(rows1);
        PageBean<Book> pageBean = new PageBean<Book>();
        //设置参数
        pageBean.setCurrentPage(currentPage);
        pageBean.setRows(rows);
        //调用FindByPageDao查询总记录数
        int totalCount = FindByPageDao.findTotalCount();
        pageBean.setTotalCount(totalCount);
        //调用FindByPageDao查询list集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        ArrayList<Book> list = FindByPageDao.findByPage(start, rows);
        pageBean.setList(list);

        //计算总页数
        int totalPage = (totalCount % rows) == 0 ? totalCount / rows : (totalCount / rows) + 1;
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }
}
